home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / D-G / GemsI / Original / 3DHashingFunction.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-16  |  1.4 KB  |  73 lines  |  [TEXT/MPS ]

  1. /*
  2. A 3D Grid Hashing Frunction
  3. by Brian Wyvill
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. /* Test Program for 3D hash function.
  8. In C the hash function can be defined in a macro which
  9. avoids a function call
  10. and the bit operations are defined in the language.
  11. */
  12.  
  13. #include "GraphicsGems.h"        
  14. #include <stdio.h>
  15. #include <math.h>
  16.  
  17. #define RANGE       256
  18. #define NBITS       4
  19. #define RBITS       4
  20. #define MASK        0360
  21. #define HASH(a,b,c) ((((a&MASK)<<NBITS|b&MASK)<<NBITS|c&MASK)>>RBITS)
  22. #define HSIZE       1<<NBITS*3
  23. #define ABS(x) (int)(x<0 ? -x : x)
  24. #define TRUE 1
  25. #define FALSE 0
  26.  
  27. typedef struct {
  28.   double x,y,z;
  29. } Triple, *RefTriple;
  30.  
  31. typedef struct {   /* linked list of objects to be stored */
  32.   Triple origin;
  33.   struct Object *link;
  34. } Object, *RefObject;
  35.  
  36. typedef struct {  /* linked list of voxels (object pointers) */
  37.   RefObject objectList;
  38.   struct Voxel *link;
  39. } Voxel, *RefVoxel;
  40.  
  41. RefVoxel table[HSIZE];  /* Table of pointers to Voxels */
  42.  
  43.  
  44. checkrange(z) double z;
  45. {
  46.   if (z < 0 || z >= RANGE) fprintf(stderr,"%f out of range\n",z),         exit();
  47. }
  48.  
  49. double getcoord()
  50. {
  51.   char buf[80];
  52.   double z;
  53.   scanf("%s",buf);
  54.   z = atof(buf);
  55.   checkrange(z);  
  56.   return z;
  57. }
  58.  
  59. main()
  60. {
  61.   Triple a;
  62.   while (TRUE) {
  63.     printf("Enter object position x y z ===> ");
  64.     a.x = getcoord();
  65.     a.y = getcoord();
  66.     a.z = getcoord();
  67.     printf("\ncoord: %d %d %d Hashes to %d\n",ABS(a.x),ABS(a.y),ABS(a.z),
  68.        HASH(ABS(a.x), ABS(a.y), ABS(a.z) ));
  69.   };  
  70. }
  71.  
  72.  
  73.